﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Validation;
using System.Diagnostics;
using ProgNetComponentsDx.Tasks;
using DevExpress.XtraEditors;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            t1.TaskPerformed += new PerformTaskHandler(t1_TaskPerformed);
            t1.TaskFinished += new EventHandler(t1_TaskFinished);

            t2.TaskPerformed += new PerformTaskHandler(t2_TaskPerformed);
            t2.TaskFinished += new EventHandler(t2_TaskFinished);
            col.Add(t1);
            col.Add(t2);
            col.RunFinished += new EventHandler(col_RunFinished);
        }
        DateTime start;

        void col_RunFinished(object sender, EventArgs e)
        {
            memoEdit1.AppendLine("czas: " + (DateTime.Now - start).ToString());
            simpleButton1.Text = " " + t1.Status.ToString();
            simpleButton2.Text = " " + t2.Status.ToString();
        }
        private BaseTaskCollection col = new BaseTaskCollection();

        void t1_TaskFinished(object sender, EventArgs e)
        {
            simpleButton1.Text += " " + t1.Status.ToString();
        }
        void t2_TaskFinished(object sender, EventArgs e)
        {
            //simpleButton2.Text += " " + t2.Status.ToString();
        }
        
        void t1_TaskPerformed(object sender, TaskResultEventArgs e)
        {
            int i = 0;
            while (i < 5)
            {
                e.Token.ThrowIfCancellationRequested();
                SetText(simpleButton1, i++);
                System.Threading.Thread.Sleep(1000);
            }
            e.Result = TaskStatus.Finished;
        }
        void t2_TaskPerformed(object sender, TaskResultEventArgs e)
        {
            int i = 0;
            while (i < 5)
            {
                e.Token.ThrowIfCancellationRequested();
                SetText(simpleButton2, i++);
                System.Threading.Thread.Sleep(1000);
                if (i == 3)
                    i += (i / (i - 3));
            }
            e.Result = TaskStatus.Finished;
        }
        private void SetText(SimpleButton btn, int i)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<SimpleButton, int>(SetText), btn, i);
                return;
            }
            btn.Text = i.ToString();
        }
      
        private BaseEventTask t1 = new BaseEventTask();
        private BaseEventTask t2 = new BaseEventTask();

        private void Start(bool Parallel)
        {
            memoEdit1.AppendLine("Parallel: " + Parallel);
            start = DateTime.Now;
            col.Parallel = Parallel;
            //col.Run();
            t1.Run(Parallel);

            DevExpress.XtraGrid.Views.Grid.GridView gv;
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Start(true);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Start(false);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            col.Cancel();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {

        }
    }
}
